package hlhj.fhp.burst.customView

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.widget.PopupWindow
import android.widget.RelativeLayout
import android.widget.Toast
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tenma.ventures.bean.utils.TMEnterUtil
import com.tenma.ventures.bean.utils.TMSharedPUtil
import hlhj.fhp.burst.Contents
import hlhj.fhp.burst.R
import hlhj.fhp.burst.activitys.GoLoginDialog
import hlhj.fhp.burst.bean.BaseBean
import kotlinx.android.synthetic.main.edit_lo.view.*

/**
 * Created by Administrator on 2018\5\17 0017.
 */
class EditPop(private val context: Context,private val bid:String,private val cid:String,private val listener:CommentSuccessListener):PopupWindow() {
    init {
        initPop()
    }

    private fun initPop() {
        this.width=RelativeLayout.LayoutParams.MATCH_PARENT
        this.height=RelativeLayout.LayoutParams.MATCH_PARENT
        this.isFocusable=true
        this.contentView=LayoutInflater.from(context).inflate(R.layout.edit_lo,null)

        contentView.loCover.setOnClickListener {
            dismiss()
        }

        contentView.btSend.setOnClickListener {
            if (TextUtils.isEmpty(TMSharedPUtil.getTMUser(context).mobile)){
                TMEnterUtil.gotoBindingmobile(context,"","","")
                return@setOnClickListener
            }
            OkGo.get<String>(Contents.PUBLICK_URL+Contents.COMMENT)
                    .params("token", TMSharedPUtil.getTMToken(context))
                    .params("burst_id",bid)
                    .params("content",contentView.etContent.text.toString())
                    .params("comment_id",cid)
                    .execute(object :StringCallback(){
                        override fun onSuccess(response: Response<String>?) {
                            val str = response?.body()
                            val bean = Gson().fromJson(str, BaseBean::class.java)
                            if (bean?.code==1){
                                listener.commmentSuccess(contentView.etContent.text.toString(),bean?.data.toString())
                                Toast.makeText(context,"评论成功",Toast.LENGTH_SHORT).show()
                            }else if (bean?.code==500){
                                GoLoginDialog(context).show()

                            }
                            dismiss()
                        }
                    })
        }

    }
    interface CommentSuccessListener{
        fun commmentSuccess(str:String,id:String)
    }
}